//
//  OAUserModel.m
//  OA
//
//  Created by Guo Rui on 2017/11/27.
//  Copyright © 2017年 GR Harry. All rights reserved.
//

#import "OAUserModel.h"
#import "NSString+Pinyin.h"

@implementation OAUserModel
@synthesize avatorImageURLString;
@synthesize userDesc;
@synthesize userName;
@synthesize phoneNumber;
@synthesize pinyin = _pinyin;
@synthesize fuzzysearch = _fuzzysearch;

+ (NSString *)jr_customTableName
{
    return @"t_user";
}

+ (NSString * _Nullable)jr_customPrimarykey
{
    return @"userId";
}

- (id)jr_customPrimarykeyValue
{
    return self.userId;
}

+ (BOOL)deleteTab{
   return J_DropTable(OAUserModel);
}


+ (NSDictionary *)modelCustomPropertyMapper {
    return @{@"avatorImageURLString":@"avatarUrl",
             @"userDesc":@"desc",
             @"phoneNumber":@"moblie"
             };
}

- (NSString *)pinyin
{
    if (!_pinyin) {
        _pinyin = [self.userName transfromToPinyin];
    }
    return _pinyin;
}

- (NSString *)fuzzysearch
{
    if (!_fuzzysearch) {
        _fuzzysearch = [self.userName transfromToFuzzysearch:self.pinyin];
    }
    return _fuzzysearch;
}

#pragma mark - Public
+ (OAUserModel *)didLoginUser
{
    NSArray *users = J_Select(OAUserModel).And(@"login").eq(@(YES)).list;
    return [users firstObject];
}

+ (BOOL)logout
{
    OAUserModel *user = [self didLoginUser];
    return J_Delete(user).updateResult;
}

- (NSString *)userDesc
{
    if (userDesc.length > 0) {
        return userDesc;
    }
    return @"他很懒，没有留下任何说明";
}

+ (NSArray *)modelPropertyBlacklist {
    return @[@"login"];
}

@end
